﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
    public class Zor_Examine_Bll<T> where T : class
    {
        #region - 审核管理列表 -

        /// <summary>
        /// 获取评议管理列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model, int index, out int TotalCount, string where)
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id; //获取当前登录人的部门(税务局)ID
            if (Model.Tenant_Name == null &&
                Model.Lease_StartTime == null &&
                Model.NatPer_Name == null &&
                Model.NatPer_Phone == null &&
                Model.NatPer_IDNumber == null &&
                Model.Acceptance_TicketNumber == null &&
                Model.Acceptance_TaxNumber == null &&
                Model.State == null &&
                Model.AcceptanceType == null  
                             
                )
            {
                var sqlSurch = "Dt_Id in ( select Dt_Id  from dbo.Sys_Department where  Dt_ParentId in (select Dt_Id  from dbo.Sys_Department where  Dt_ParentId in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + "))) and Examine_State in(0)";
                return PagerHelp<T>.GetPagerList(index, "V_Zor_Examine", "Evaluate_Date", out TotalCount, sqlSurch);
            }
            return PagerHelp<T>.GetPagerList(index, "V_Zor_Examine", "Evaluate_Date desc ", out TotalCount, GetSearchWhere(Model, "Dt_Id in ( select Dt_Id  from dbo.Sys_Department where  Dt_ParentId in (select Dt_Id  from dbo.Sys_Department where  Dt_ParentId in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + RequestSession.GetSessionUser().DT_Id + "))) and Examine_State<>3"));
        }

       /// <summary>
       /// 根据审核ID 获取审核信息
       /// </summary>
       /// <param name="Examine_Id"></param>
       /// <returns></returns>
        public static List<T> getExamineInfo(int Examine_Id)
        {
            return Zor_Examine_DAL<T>.getExamineInfo(Examine_Id);
        }

        /// <summary>
        /// 审核
        /// create zhl by 17.3.31
        /// </summary>
        /// <param name="examine">审核对象</param>
        /// <param name="rentMoney">评定租金</param>
        /// <returns>2--当前评议已推送-1操作失败1--操作成功的标识</returns>
        public static BaseAnswer Examine(Zor_Examine examine, string accpid, double rentMoney)
        {
            BaseAnswer bar = Zor_Examine_DAL<T>.Examine(examine, accpid, rentMoney);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "审核成功！" : "审核失败！";
            return bar;  
        
        }
        /// <summary>
        /// 退回至评定
        /// create zhl by 17.3.31
        /// 评定状态(0正在评定,1已评定,2退回评定)
        /// </summary>
        /// <param name="aptid">受理单id</param>
        /// <returns></returns>
        public static BaseAnswer ReturnEvaluate(Zor_Examine examine, string accpid)
        {

            BaseAnswer bar = Zor_Examine_DAL<T>.ReturnEvaluate(examine, accpid);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "退回成功！" : "退回失败！";
            return bar;        
        }
        #region 私有函数

        private static string GetSearchWhere(dynamic Model, string where)
        {
            var sqlSurch = string.Empty;

            if (Model.Tenant_Name != null)
            {
                sqlSurch += " and Tenant_Name like '%" + Model.Tenant_Name + "%'";
            }
            if (Model.Lease_StartTime != null && Model.Lease_EndTime != null)
            {
                sqlSurch += " and Lease_StartTime >=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_StartTime.ToString())) + " and Lease_EndTime<=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_EndTime.ToString())) + "";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += " and NatPer_Name like '%" + Model.NatPer_Name + "%'";
            }
            if (Model.NatPer_Phone != null)
            {
                sqlSurch += " and NatPer_Phone = '" + Model.istaxregistration + "'";
            }
            if (Model.NatPer_IDNumber != null)
            {
                sqlSurch += " and NatPer_IDNumber = '" + Model.NatPer_IDNumber + "'";
            }
            if (Model.Acceptance_TicketNumber != null)
            {
                sqlSurch += " and Acceptance_TicketNumber = '" + Model.Acceptance_TicketNumber + "'";
            }
            if (Model.State != null)
            {
                if (Model.State == "全部")
                {
                    sqlSurch += " and State in ('已保存','已审核','退回至评定','被退回')";     
                }
                else
                {
                    sqlSurch += " and State = '" + Model.State + "'";
                }           
            }
            if (Model.AcceptanceType != null)
            {
                if (Model.AcceptanceType == "全部")
                {
                    sqlSurch += " and AcceptanceType in('社区','自然人')";
                }
                else
                {
                    sqlSurch += " and AcceptanceType = '" + Model.AcceptanceType + "' ";
                }
            }
            return where + sqlSurch;
        }
        #endregion
        #endregion
    }
}
